﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Space
{
    public enum ShipClass
    {
        Corvette = 4, Destroyer = 3, Cruiser = 2, Battleship = 1, Carrier = -1, Colony = -2
    }

    public class CapitalShip : NewtonShip, IBuildable
    {
        public bool Colony { get; set; }
        public bool Built { get; set; }
        public float BuildingTime { get; set; }

        public ShipClass Class { get; set; }

        public Vector3 FormationPoint { get; set; }

        float angle;

        List<FighterShip> fighters;

        public int FighterNum
        {
            get {
                if (fighters == null) return 0;
                return fighters.Capacity;
            }
            set {
                if (value == 0)
                {
                    if (fighters != null)
                        fighters.Clear();
                    return;
                }
                if (fighters == null)
                {
                    fighters = new List<FighterShip>(value);
                }
                if (value < fighters.Count)
                {
                    fighters.RemoveRange(value, fighters.Count - value);
                }
                fighters.Capacity = value;
            }
        }
        public override float Angle
        {
            get
            {
                if(Action == "colonize")
                    return (float)Math.Atan2(ActionTarget.Position.Z - Position.Z, ActionTarget.Position.X - Position.X);

                return base.Angle;
                //return (float)Math.Atan2(Fleet.Position.Z - Position.Z, Fleet.Position.X - Position.X);
            }
        }

        List<Gun> guns = new List<Gun>();
        
        public CapitalShip(Game game)
            : base(game)
        {
        }

        public void AddGun(Gun gun)
        {
            guns.Add(gun);
        }

        public override void Update(float time)
        {
            if (!Built) return;

            if (Alive)
            {
                if (Action == "colonize")
                {
                    Planet planet = ActionTarget as Planet;

                    Vector3 dest = ActionTarget.Position;
                    float distanceSquare = (dest - Position).LengthSquared();

                    if (distanceSquare < planet.Radius * planet.Radius)
                    {
                        if (planet.Player.Neutral)
                        {
                            planet.Population = 0;
                            planet.Player.RemovePlanet(planet);
                            planet.Player = Player;
                            planet.Player.AddPlanet(planet);
                        }
                        else planet.Population--;

                        Alive = false;
                        NoExplode = true;
                    }
                    else
                    {
                        Vector3 dir = dest - Position;
                        dir.Normalize();
                        Position += dir * Speed * time;
                    }
                }
                else
                {
                    pushCooldown -= time;
                    if (pushCooldown <= 0)
                    {
                        Vector3 dest;
                        //dest = Fleet.Position;
                        dest = FormationPoint;

                        SetDestination(dest);

                        Destination = new PositionSized
                        {
                            Position = dest,
                            Radius = Radius
                        };
                        //float distanceSquare = (dest - Position).LengthSquared();
                        ////if (distanceSquare > Fleet.Size * 4 / 6)
                        //{
                        //    if (distanceSquare > Speed * Speed)
                        //    {
                        //        Vector3 dir = dest - Position;
                        //        dir.Normalize();
                        //        angle = (float)Math.Atan2(dir.Z, dir.X);
                        //        Position += dir * Speed * (Hyperdrive ? HyperdriveMultiplier : 1) * time;
                        //    }
                        //    else
                        //    {
                        //        Position = dest;
                        //    }
                        //}
                    }
                }
                //fire += time;
                //if (fire >= FireInterval && Target != null)
                //{
                //    Fire();
                //    fire = 0;
                //}
                foreach (Gun gun in guns)
                {
                    gun.Position = Position;
                    gun.Update(time);

                    if(Target!=null) {
                    if ((Target.Position - Position).Length() > Range || !Target.Alive) Target = null;

                        if (gun.CanFire)
                        {
                            gun.Target = Target;
                            gun.Fire();
                        }
                    }
                }
            }
            

            base.Update(time);
        }
    }
}
